In this demonstration,
you will learn about asynchronous communication
between order and customer. Go to the Order Microservices and create an orderModel.js file and orderRouter.js file. You can also create a DAO
service and Controller layer. But here, you are
going to work with the orderModel.js file and
orderRouter.js file. First, import Mongoose. Create orderSchema and
callSchema method on mongoose. The customerId is
auto-generated by MongoDB. Remove the initial date. OrderModel has productId
and customerId only. Create the model and
export the model. Now you will work
with the orderRouter. First, import the
express module, get the router object, import the mongoose
module and axios module. Connect with dbconfig and
import the order module. Now create the router.post and the order object by retrieving the order data from
the request body. First, get the customerId which is the
req.body.customerId. The customerId will be for
mongoose.Types.ObjectId. Similarly, get the productId, remove all the logs. Save the order object, and save the return value
in a variable data. If the data is saved,
returning some value, then you will get
the response status as 200 with a success message. Inside the catch, the
error will be printed. Now you will have
another method, router.get, where you
have to pass the id. The router.get method has the request and
response parameters. Call the order by ID, taking request params as ID, pass the new order object. If the order exists, then through axios.get method, you have to pass the
URL of the customer. From the Postman, you have to request the get id. Internally, the request
will call the service and pass the customerId which
you have retrieved. If you get the response, then store the response inside the customer name as the response will be
the new order object. In case of success, call the product as well. Order service internally calls customer service and
product service. Similarly, store the product
name and product details. If both are a success, then send responce.json
as the orderObject. For the else block, add statuses 400 with an
error message and a catch. Export the model.router. You will need an
index.js file also, which will import the express
module and orderRouter. Use express.json and
orderRouter listening on 5000. Now, start the terminal. Make sure that all
the services are up. First, check the customer layer and check that the customer
layer is listening. Go to the product layer, which is already listening, go to the order service. Inside the router, make sure you give
the correct path for the dbconfig connection. Install axios as well. Inside the package.json file, check that the
axios is installed. Now, run and check that the order service is
listening and connected. Now, send the request
through Postman. Start MongoDB as well. Add first show databases. Here, OrderDb is being created. Use OrderDb and add
show collections. Three tables will be created, customer, order, and product. Add db.customer.find,
save the data 101. Now, check what is
inside the product. You get the details like
Samsung mobile and the ID. Work with the Order
microservice, send the request
from the Postman. First, save the order, copy the productId
and customerId. Send the request to check
if it is a success. Now, if we add db.orders.find, you get the saved
customerId and productId, you also get the orderId. Copy the orderId and open
a new tab, paste the id. The request calls the router, so passing the id
calls customer on the product service by passing the customerId and productId. Once you send the request, you can see that when you
are passing the orderId, you are getting
the customer name and the product details. The order services internally calling the product service, and the customer service is asynchronous
communication that is happening between order
and customer. Thank you.